import { LineBreaker } from 'css-line-break'
// 可以出现在行尾的标点符号
// const PunctuationEnd = '。？！，、；：’”）》】〕』」'
// 可以出现在行首的标点符号
const PunctuationStart = '“（《【〔『「'

function MyWordBreak (text, options) {
    const breaker = LineBreaker(text, options)
    let words = []
    let bk
    while (!(bk = breaker.next()).done) {
        let word = bk.value.slice()
        // console.log(word)
        words = words.concat(breakWordExt(word))
    }
    console.log(text)
    console.log(words)
    return words
}

function breakWordExt (inputWord) {
    let words = [] // 返回数组
    let tmpArr = [inputWord]
    do {
        let word = tmpArr.shift()
        if (word.indexOf('：“') > -1) {
            // 例："说：“嗯”" 拆分为 "说：" 和 "“嗯”"
            tmpArr.unshift(word.substring(word.indexOf('：“') + 1))
            tmpArr.unshift(word.substring(0, word.indexOf('：“') + 1))
        } else if (word.length > 3 && PunctuationStart.indexOf(word[1]) > -1 && PunctuationStart.indexOf(word[0]) === -1) {
            // 第二个字符是引号、括号之类，第一个不是
            // 例："为“50%”"
            words.push(word.substring(0, 1))
            tmpArr.unshift(word.substring(1))
        // } else if (word.lastIndexOf('“') > 0) {
        //     // 例："为“50%”" 拆分为 "为" 和 "“50%”"
        //     tmpArr.unshift(word.substring(word.lastIndexOf('“')))
        //     tmpArr.unshift(word.substring(0, word.lastIndexOf('“')))
        } else if (word.indexOf('、') > -1 && word.indexOf('、') !== word.length - 1) {
            // 例："“xMinYMin”、“xMidYMid”和“xMaxYMax”等，" 拆分为 "“xMinYMin”、" 和 "“xMidYMid”和“xMaxYMax”等，"
            tmpArr.unshift(word.substring(word.indexOf('、') + 1))
            tmpArr.unshift(word.substring(0, word.indexOf('、') + 1))
        } else {
            words.push(word)
        }
    } while (tmpArr.length > 0)
    return words
}

export { MyWordBreak }